package com.itheima.test;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class test3 {

    public static void main(String[] args) throws Exception {
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File("C:\\Users\\Jason\\Desktop\\新建 XLSX 工作表.xlsx")));
        XSSFSheet sheet = workbook.getSheetAt(0);
        for (Row row : sheet) {
            for (Cell cell : row) {

                switch (cell.getCellType()){
                    case Cell.CELL_TYPE_STRING:
                        System.out.println(cell.getStringCellValue());
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        //日期和数字都会被识别为number类型
                        //进来后首先判断 用工具类判断是否为日期Date类型 如果是 把他转为想要的date类型
                        if (HSSFDateUtil.isCellDateFormatted(cell)){
                            Date date = cell.getDateCellValue();
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                            String format = sdf.format(date);
                            System.out.println("format = " + format);
                        }else {
                            //如果是数值 打印出来是douboo类型 带小数点的 那我们不想要小数点
                            //就需要先把他转换为string类型 这样他的小数点就消失了 再打印
                            cell.setCellType(Cell.CELL_TYPE_STRING);
                            System.out.println("cell = " + cell);
                            System.out.println(cell.getStringCellValue());
                        }
                }


            }
        }



            }

        }



